package com.woniuxy.yogaapp.service.imp;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.yogaapp.mapper.ChatMapper;

import com.woniuxy.yogaapp.mapper.ClubCoachMapper;

import com.woniuxy.yogaapp.mapper.UserRolesMapper;

import com.woniuxy.yogaapp.mapper.YogaClubMapper;
import com.woniuxy.yogaapp.pojo.Chat;
import com.woniuxy.yogaapp.pojo.ClubCoach;
import com.woniuxy.yogaapp.pojo.YogaClub;
import com.woniuxy.yogaapp.service.ChatService;
import com.woniuxy.yogaapp.service.YogaClubService;
import com.woniuxy.yogaapp.util.AdminPhone;
import com.woniuxy.yogaapp.util.SMSUtil;
import com.woniuxy.yogaapp.util.TimeUtil;

import lombok.Data;

@Data
@Service
@Transactional
public class YogaClubServiceImp implements YogaClubService {

	@Resource
	private YogaClubMapper yogaClubMapper;

	@Resource
	private ChatMapper chatMapper;

	@Resource
	private ChatService chatService;

	@Resource
	private ClubCoachMapper clubCoachMapper;

	@Resource
	private UserRolesMapper userRolesMapper;

	// 查询所有会馆
	@Override
	public List<YogaClub> findAllYogaClub() {
		return yogaClubMapper.findAllYogaClub();
	}

	// 根据会馆id查询教练信息再查询学员信息
	@Override
	public YogaClub findByYid(int bid) {
		return yogaClubMapper.findByYid(bid);
	}

	// 同意场馆授权
	@Override
	public String agreeYogaClub(Chat chat) {
		String result = "请求失败";
		int yuid = chat.getSender();
		int chatid = chat.getChatid();
		int row = yogaClubMapper.agreeYogaClub(yuid);
		if (row > 0) {
			chat.setDeal("同意");
			int row2 = chatMapper.changeDealAgree(chatid);
			int row4 = userRolesMapper.deleteRoles1(yuid);
			// 增加通知消息
			int sender = 1;
			int recipient = yuid;
			String mes = "已同意认证";
			String state = "未读";
			String mestime = TimeUtil.getNowDate();
			int mestype = 2;
			Chat chat2 = new Chat(sender, recipient, mes, state, mestime, mestype);
			int row5 = chatMapper.addChat(chat2);
			if (row2 > 0 && row4 > 0 && row5 > 0) {
				int row3 = yogaClubMapper.setYogaClub(yuid);
				int row6 = chatMapper.changeStatetoReady(chatid);
				if (row3 > 0 && row6 > 0) {
					result = "请求成功，已同意场馆认证";
				}
			}
		}
		return result;
	}

	// 驳回场馆授权
	@Override
	public String disagreeYogaClub(Chat chat) {
		String result = "请求失败";
		int yuid = chat.getSender();
		int chatid = chat.getChatid();
		int row = yogaClubMapper.disagreeYogaClub(yuid);
		if (row > 0) {
			chat.setDeal("驳回");
			int row2 = chatMapper.changeDealDisAgree(chatid);
			// 增加通知消息
			int sender = 1;
			int recipient = yuid;
			String mes = "已驳回认证";
			String state = "未读";
			String mestime = TimeUtil.getNowDate();
			int mestype = 2;
			Chat chat2 = new Chat(sender, recipient, mes, state, mestime, mestype);
			int row3 = chatMapper.addChat(chat2);
			int row6 = chatMapper.changeStatetoReady(chatid);
			if (row2 > 0 && row3 > 0 && row6 > 0) {
				result = "请求成功，已驳回场馆认证";
			}
		}
		return result;
	}

	// 根据会馆id查询教练信息
	@Override
	public YogaClub findByCoach(int yid) {

		return yogaClubMapper.findByCoach(yid);
	}

	// 根据uid查询 该场馆信息
	@Override
	public YogaClub findYogaClubByUid(YogaClub yogaClub) {

		return yogaClubMapper.findYogaClubByUid(yogaClub);
	}

	// 添加 场馆基本信息
	@Override
	public String addYogaClubInfo(YogaClub yogaClub) {
		String result = "添加失败！";
		int row = yogaClubMapper.addYogaClubInfo(yogaClub);
		if (row == 1) {
			result = "添加成功！";
		}
		return result;
	}

	// 老用户修改场馆信息
	@Override
	public String updateYogaClubInfo(YogaClub yogaClub) {
		String result = "修改失败！";
		int row = yogaClubMapper.updateYogaClubInfo(yogaClub);
		if (row == 1) {
			result = "修改成功！";
		}
		return result;
	}

	// 填写 认证信息 给管理员发送通知 与短信
	@Override
	public String approvePower(YogaClub yogaClub) {
		String result = "认证请求发送失败！";
		// 1.添加认证信息
		int row = yogaClubMapper.updateYogaClubApprove(yogaClub);
		// 2.给管理员发送通知
		// 2.1查询该用户的所有信息
		YogaClub yogaClub1 = yogaClubMapper.findYogaClubByUid(yogaClub);

		// 2.2发送请求认证消息
		int row1 = chatService.yogaClubToAdmin(yogaClub1);
		// 3.发送短信给管理员
		String phoneNumber = AdminPhone.getAdminTel();
		String[] params = { yogaClub1.getLegalperson(), yogaClub1.getYname() + "瑜伽场馆" };
		SMSUtil.addInformToAdmin(phoneNumber, params);

		if (row == 1 && row1 == 1) {
			result = "请求发成功！";
		}
		return result;
	}

	/*
	 * 场馆拒绝教练加入
	 */
	@Override
	public String refuseApplyFor(Chat chat) {
		String result = "处理失败！";
		// 1.改变该消息改为已读
		chat.setState("已读");
		int row = chatMapper.changeState9(chat);

		// 2改变 clubcoach approve属性
		int cuid = chat.getSender();// 发送人uid
		int yuid = chat.getRecipient();// 接受人uid
		ClubCoach clubCoach = new ClubCoach(yuid, cuid, 1);

		int row1 = clubCoachMapper.updateClubApprove(clubCoach);
		if (row > 0 && row1 > 0) {
			result = "处理成功！";
		}
		return result;
	}

	/*
	 * 场馆同意教练加入
	 */
	@Override
	public String agreeApplyFor(Chat chat) {
		String result = "处理失败！";
		// 1.改变该消息改为已读
		chat.setState("已读");
		int row = chatMapper.changeState9(chat);

		// 2改变 clubcoach approve属性
		int cuid = chat.getSender();// 发送人uid
		int yuid = chat.getRecipient();// 接受人uid
		ClubCoach clubCoach = new ClubCoach(yuid, cuid, 2);

		int row1 = clubCoachMapper.updateClubApprove(clubCoach);
		if (row > 0 && row1 > 0) {
			result = "处理成功！";
		}
		return result;
	}

}
